package com.slipper.bpmn;

import java.util.Iterator;

import com.slipper.bpmn.model.Bpmn;
import com.slipper.bpmn.parser.ParserFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BpmnHelper {
    private static final Logger logger = LoggerFactory.getLogger(BpmnHelper.class);
    
    public static Bpmn parser(String xml) {
        Bpmn bpmn = new Bpmn();
        
        try {
            Document doc = DocumentHelper.parseText(xml);
            Element root = doc.getRootElement();
            Element process = root.element("process");
            Iterator<Element> iter = process.elementIterator();
            while(iter.hasNext()) {
                try {
                    bpmn.add(ParserFactory.parser(iter.next()));
                } catch(Exception e) {
                    bpmn.appendError(e.getMessage());
                }
            }
        } catch(Exception e) {
            logger.error("xml解释出错！", e);
            bpmn.appendError(e.getMessage());
        }
        
        bpmn.validate();
        return bpmn;
    }

    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<bpmn2:definitions xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                + "     xmlns:bpmn2=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" "
                + "     xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\" "
                + "     xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\" "
                + "     xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\" "
                + "     xmlns:bioc=\"http://bpmn.io/schema/bpmn/biocolor/1.0\" "
                + "     id=\"sample-diagram\" "
                + "     targetNamespace=\"http://bpmn.io/schema/bpmn\" "
                + "     xsi:schemaLocation=\"http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd\">"
                + "  <bpmn2:process id=\"Process_1\" isExecutable=\"false\">"
                + "    <bpmn2:startEvent id=\"Event_1j8835g\" name=\"开始节点\">"
                + "      <bpmn2:outgoing>Flow_0d49ab8</bpmn2:outgoing>"
                + "    </bpmn2:startEvent>"
                + "    <bpmn2:task id=\"Activity_0xqb8fc\" name=\"拟稿\">"
                + "      <bpmn2:incoming>Flow_0d49ab8</bpmn2:incoming>"
                + "      <bpmn2:incoming>Flow_19k7gnv</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_1dfd39q</bpmn2:outgoing>"
                + "    </bpmn2:task>"
                + "    <bpmn2:sequenceFlow id=\"Flow_0d49ab8\" sourceRef=\"Event_1j8835g\" targetRef=\"Activity_0xqb8fc\" />"
                + "    <bpmn2:task id=\"Activity_16gxqc1\" name=\"部门审批\" color=\"#ff8000\" employeeIds=\"6@20\">"
                + "      <bpmn2:incoming>Flow_1dfd39q</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_1farnzy</bpmn2:outgoing>"
                + "    </bpmn2:task>"
                + "    <bpmn2:exclusiveGateway id=\"Gateway_0vfbehd\" name=\"分支节点\">"
                + "      <bpmn2:incoming>Flow_1farnzy</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_19k7gnv</bpmn2:outgoing>"
                + "      <bpmn2:outgoing>Flow_1rpxojw</bpmn2:outgoing>"
                + "    </bpmn2:exclusiveGateway>"
                + "    <bpmn2:sequenceFlow id=\"Flow_19k7gnv\" name=\"不通过\" sourceRef=\"Gateway_0vfbehd\" targetRef=\"Activity_0xqb8fc\" branchType=\"=\" branchValue=\"1\" />"
                + "    <bpmn2:parallelGateway id=\"Gateway_14gxfoa\" name=\"并行开始节点\">"
                + "      <bpmn2:incoming>Flow_1rpxojw</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_04l0p6i</bpmn2:outgoing>"
                + "      <bpmn2:outgoing>Flow_0cjqidk</bpmn2:outgoing>"
                + "      <bpmn2:outgoing>Flow_1t8as5l</bpmn2:outgoing>"
                + "    </bpmn2:parallelGateway>"
                + "    <bpmn2:task id=\"Activity_0xk5k3l\" name=\"原部门审批\">"
                + "      <bpmn2:incoming>Flow_04l0p6i</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_1cvmnx0</bpmn2:outgoing>"
                + "    </bpmn2:task>"
                + "    <bpmn2:task id=\"Activity_0xtnq0f\" name=\"人事科审批\">"
                + "      <bpmn2:incoming>Flow_0cjqidk</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_03z4m1z</bpmn2:outgoing>"
                + "    </bpmn2:task>"
                + "    <bpmn2:task id=\"Activity_0skse74\" name=\"财务科审批\">"
                + "      <bpmn2:incoming>Flow_1t8as5l</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_0jggkaf</bpmn2:outgoing>"
                + "    </bpmn2:task>"
                + "    <bpmn2:complexGateway id=\"Gateway_0dxqt9j\" name=\"并行结束节点\">"
                + "      <bpmn2:incoming>Flow_03z4m1z</bpmn2:incoming>"
                + "      <bpmn2:incoming>Flow_0jggkaf</bpmn2:incoming>"
                + "      <bpmn2:incoming>Flow_1cvmnx0</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_1tkvxnh</bpmn2:outgoing>"
                + "    </bpmn2:complexGateway>"
                + "    <bpmn2:task id=\"Activity_0vifn9h\" name=\"局领导审批\">"
                + "      <bpmn2:incoming>Flow_1tkvxnh</bpmn2:incoming>"
                + "      <bpmn2:outgoing>Flow_1hjp0yh</bpmn2:outgoing>"
                + "    </bpmn2:task>"
                + "    <bpmn2:endEvent id=\"Event_1llwfka\" name=\"结束节点\">"
                + "      <bpmn2:incoming>Flow_1hjp0yh</bpmn2:incoming>"
                + "    </bpmn2:endEvent>"
                + "    <bpmn2:sequenceFlow id=\"Flow_04l0p6i\" sourceRef=\"Gateway_14gxfoa\" targetRef=\"Activity_0xk5k3l\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_0cjqidk\" sourceRef=\"Gateway_14gxfoa\" targetRef=\"Activity_0xtnq0f\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_03z4m1z\" sourceRef=\"Activity_0xtnq0f\" targetRef=\"Gateway_0dxqt9j\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_1tkvxnh\" sourceRef=\"Gateway_0dxqt9j\" targetRef=\"Activity_0vifn9h\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_1hjp0yh\" sourceRef=\"Activity_0vifn9h\" targetRef=\"Event_1llwfka\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_0jggkaf\" sourceRef=\"Activity_0skse74\" targetRef=\"Gateway_0dxqt9j\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_1cvmnx0\" sourceRef=\"Activity_0xk5k3l\" targetRef=\"Gateway_0dxqt9j\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_1t8as5l\" sourceRef=\"Gateway_14gxfoa\" targetRef=\"Activity_0skse74\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_1rpxojw\" name=\"通过\" sourceRef=\"Gateway_0vfbehd\" targetRef=\"Gateway_14gxfoa\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_1dfd39q\" sourceRef=\"Activity_0xqb8fc\" targetRef=\"Activity_16gxqc1\" />"
                + "    <bpmn2:sequenceFlow id=\"Flow_1farnzy\" sourceRef=\"Activity_16gxqc1\" targetRef=\"Gateway_0vfbehd\" />"
                + "  </bpmn2:process>"
                + "  <bpmndi:BPMNDiagram id=\"BPMNDiagram_1\">"
                + "    <bpmndi:BPMNPlane id=\"BPMNPlane_1\" bpmnElement=\"Process_1\">"
                + "      <bpmndi:BPMNEdge id=\"Flow_1farnzy_di\" bpmnElement=\"Flow_1farnzy\">"
                + "        <di:waypoint x=\"630\" y=\"120\" />"
                + "        <di:waypoint x=\"685\" y=\"120\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_1dfd39q_di\" bpmnElement=\"Flow_1dfd39q\">"
                + "        <di:waypoint x=\"480\" y=\"120\" />"
                + "        <di:waypoint x=\"530\" y=\"120\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_1rpxojw_di\" bpmnElement=\"Flow_1rpxojw\">"
                + "        <di:waypoint x=\"735\" y=\"120\" />"
                + "        <di:waypoint x=\"845\" y=\"120\" />"
                + "        <bpmndi:BPMNLabel>"
                + "          <dc:Bounds x=\"779\" y=\"102\" width=\"22\" height=\"14\" />"
                + "        </bpmndi:BPMNLabel>"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_1t8as5l_di\" bpmnElement=\"Flow_1t8as5l\">"
                + "        <di:waypoint x=\"870\" y=\"145\" />"
                + "        <di:waypoint x=\"870\" y=\"180\" />"
                + "        <di:waypoint x=\"700\" y=\"180\" />"
                + "        <di:waypoint x=\"700\" y=\"215\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_1cvmnx0_di\" bpmnElement=\"Flow_1cvmnx0\">"
                + "        <di:waypoint x=\"1040\" y=\"265\" />"
                + "        <di:waypoint x=\"1040\" y=\"320\" />"
                + "        <di:waypoint x=\"895\" y=\"320\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_0jggkaf_di\" bpmnElement=\"Flow_0jggkaf\">"
                + "        <di:waypoint x=\"700\" y=\"265\" />"
                + "        <di:waypoint x=\"700\" y=\"320\" />"
                + "        <di:waypoint x=\"845\" y=\"320\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_1hjp0yh_di\" bpmnElement=\"Flow_1hjp0yh\">"
                + "        <di:waypoint x=\"870\" y=\"435\" />"
                + "        <di:waypoint x=\"870\" y=\"472\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_1tkvxnh_di\" bpmnElement=\"Flow_1tkvxnh\">"
                + "        <di:waypoint x=\"870\" y=\"345\" />"
                + "        <di:waypoint x=\"870\" y=\"385\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_03z4m1z_di\" bpmnElement=\"Flow_03z4m1z\">"
                + "        <di:waypoint x=\"870\" y=\"265\" />"
                + "        <di:waypoint x=\"870\" y=\"295\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_0cjqidk_di\" bpmnElement=\"Flow_0cjqidk\">"
                + "        <di:waypoint x=\"870\" y=\"145\" />"
                + "        <di:waypoint x=\"870\" y=\"215\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_04l0p6i_di\" bpmnElement=\"Flow_04l0p6i\">"
                + "        <di:waypoint x=\"870\" y=\"145\" />"
                + "        <di:waypoint x=\"870\" y=\"180\" />"
                + "        <di:waypoint x=\"1040\" y=\"180\" />"
                + "        <di:waypoint x=\"1040\" y=\"215\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_19k7gnv_di\" bpmnElement=\"Flow_19k7gnv\">"
                + "        <di:waypoint x=\"710\" y=\"95\" />"
                + "        <di:waypoint x=\"710\" y=\"60\" />"
                + "        <di:waypoint x=\"430\" y=\"60\" />"
                + "        <di:waypoint x=\"430\" y=\"95\" />"
                + "        <bpmndi:BPMNLabel>"
                + "          <dc:Bounds x=\"554\" y=\"42\" width=\"33\" height=\"14\" />"
                + "        </bpmndi:BPMNLabel>"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNEdge id=\"Flow_0d49ab8_di\" bpmnElement=\"Flow_0d49ab8\">"
                + "        <di:waypoint x=\"278\" y=\"120\" />"
                + "        <di:waypoint x=\"380\" y=\"120\" />"
                + "      </bpmndi:BPMNEdge>"
                + "      <bpmndi:BPMNShape id=\"Event_1j8835g_di\" bpmnElement=\"Event_1j8835g\">"
                + "        <dc:Bounds x=\"242\" y=\"102\" width=\"36\" height=\"36\" />"
                + "        <bpmndi:BPMNLabel>"
                + "          <dc:Bounds x=\"238\" y=\"141\" width=\"43\" height=\"14\" />"
                + "        </bpmndi:BPMNLabel>"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Activity_0xqb8fc_di\" bpmnElement=\"Activity_0xqb8fc\">"
                + "        <dc:Bounds x=\"380\" y=\"95\" width=\"100\" height=\"50\" />"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Activity_16gxqc1_di\" bpmnElement=\"Activity_16gxqc1\" bioc:stroke=\"#ff8000\">"
                + "        <dc:Bounds x=\"530\" y=\"95\" width=\"100\" height=\"50\" />"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Gateway_0vfbehd_di\" bpmnElement=\"Gateway_0vfbehd\" isMarkerVisible=\"true\">"
                + "        <dc:Bounds x=\"685\" y=\"95\" width=\"50\" height=\"50\" />"
                + "        <bpmndi:BPMNLabel>"
                + "          <dc:Bounds x=\"688.5\" y=\"155\" width=\"43\" height=\"14\" />"
                + "        </bpmndi:BPMNLabel>"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Gateway_14gxfoa_di\" bpmnElement=\"Gateway_14gxfoa\">"
                + "        <dc:Bounds x=\"845\" y=\"95\" width=\"50\" height=\"50\" />"
                + "        <bpmndi:BPMNLabel>"
                + "          <dc:Bounds x=\"837.5\" y=\"155\" width=\"65\" height=\"14\" />"
                + "        </bpmndi:BPMNLabel>"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Activity_0xk5k3l_di\" bpmnElement=\"Activity_0xk5k3l\">"
                + "        <dc:Bounds x=\"990\" y=\"215\" width=\"100\" height=\"50\" />"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Activity_0xtnq0f_di\" bpmnElement=\"Activity_0xtnq0f\">"
                + "        <dc:Bounds x=\"820\" y=\"215\" width=\"100\" height=\"50\" />"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Activity_0skse74_di\" bpmnElement=\"Activity_0skse74\">"
                + "        <dc:Bounds x=\"650\" y=\"215\" width=\"100\" height=\"50\" />"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Gateway_0dxqt9j_di\" bpmnElement=\"Gateway_0dxqt9j\">"
                + "        <dc:Bounds x=\"845\" y=\"295\" width=\"50\" height=\"50\" />"
                + "        <bpmndi:BPMNLabel>"
                + "          <dc:Bounds x=\"837.5\" y=\"353\" width=\"65\" height=\"14\" />"
                + "        </bpmndi:BPMNLabel>"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Activity_0vifn9h_di\" bpmnElement=\"Activity_0vifn9h\">"
                + "        <dc:Bounds x=\"820\" y=\"385\" width=\"100\" height=\"50\" />"
                + "      </bpmndi:BPMNShape>"
                + "      <bpmndi:BPMNShape id=\"Event_1llwfka_di\" bpmnElement=\"Event_1llwfka\">"
                + "        <dc:Bounds x=\"852\" y=\"472\" width=\"36\" height=\"36\" />"
                + "        <bpmndi:BPMNLabel>"
                + "          <dc:Bounds x=\"848\" y=\"511\" width=\"43\" height=\"14\" />"
                + "        </bpmndi:BPMNLabel>"
                + "      </bpmndi:BPMNShape>"
                + "    </bpmndi:BPMNPlane>"
                + "  </bpmndi:BPMNDiagram>"
                + "</bpmn2:definitions>";
        Bpmn bpmn = parser(xml);
        System.out.println(bpmn);
    }
}
